<template>
  <el-dropdown>
    <span class="el-dropdown-link">
      <el-icon class="el-icon--right">
        <arrow-down />
      </el-icon>
    </span>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item @click="editNd()" >结构</el-dropdown-item>
        <el-dropdown-item @click="editLayout()">界面</el-dropdown-item>
        <el-dropdown-item @click="editDbMap()" v-if = "hasDbMapCode">映射</el-dropdown-item>
        <el-dropdown-item @click="editFunction()" v-if = "hasFunctionCode">功能</el-dropdown-item>

      </el-dropdown-menu>
    </template>
  </el-dropdown>
</template>

<script>
import { ArrowDown } from '@element-plus/icons-vue'
import { getCurrentInstance, computed } from 'vue';
import counterManage from '../../../cache/CounterManage'
import ConstDef from '../../../js/ConstDef'
import bus from 'vue3-eventbus'
export default {
  name: 'MetaDropdown',
  components: {
    ArrowDown
  },
  props: {
    nvsData: {
      type: Object,
      default: null
    }
  },
  setup(props) {
    const currentInstance = getCurrentInstance();
    const hasDbMapCode = computed(() => {
      return !!props.nvsData.nd.dbMapCode
    })
    const hasFunctionCode = computed(() => {
      let parent = currentInstance.parent;
      while (parent) {
        if (parent.proxy && parent.proxy.getFunctionConfig && parent.proxy.getFunctionConfig().config.id > 0) {
          return true;
        } else {
          parent = parent.parent;
        }
      }
      return false;
    })

    function editLayout () {
      const layoutCode = props.nvsData.layout.code;
      edit(ConstDef.ND_CODE_LAYOUT, ConstDef.LAYOUT_CODE_LAYOUT, "code", layoutCode)
    }
    function editNd () {
      const ndCode = props.nvsData.nd.ndCode;
      edit(ConstDef.ND_CODE_ND, ConstDef.LAYOUT_CODE_ND, "ndCode", ndCode)
    }
    function editDbMap () {
      const dbMapCode = props.nvsData.nd.dbMapCode;
      if (!dbMapCode) return;
      edit(ConstDef.ND_CODE_TYPE_MAPPING_ND, ConstDef.LAYOUT_CODE_DB_MAP, "dbMapCode", dbMapCode)
    }
    function getPlatform() {
      let parent = currentInstance.parent;
      while (parent != null) {
        if (parent.type.name === "PlatformConsole") { break; }
        parent = parent.parent;
      }
      return parent.proxy;
    }
    function edit(ndCode, layoutCode, codeFdName, code) {
      const tabPage = {
        name: "object view" + counterManage.getCounter(),
        title: "对象查看",
        moduleCode: ConstDef.MODULE_CODE_ENTRY,
        config: {
          nvId: 0,
          nvCode: code,
          codeFdName: codeFdName,
          ndCode: ndCode,
          layoutCode: layoutCode,
          action: 'query'
        }
      };
      const platform = getPlatform();
      platform.onTabPage(tabPage);
    }
    function editFunction () {
      let parent = currentInstance.parent;
      while (parent) {
        if (parent.proxy.getFunctionConfig) {
          break;
        } else {
          parent = parent.parent;
        }
      }
      const functionConfig = parent.proxy.getFunctionConfig();
      if (functionConfig.moduleCode === ConstDef.MODULE_CODE_ENTRY) {
        const id = functionConfig.config.id;
        const layoutCode = "entryMvDnaCodeEL01";
        const tabPage = {
          name: "entryFunctionEdit",
          title: "查询功能编辑",
          moduleCode: ConstDef.MODULE_CODE_ENTRY,
          config: {
            nvId: id,
            ndCode: 'entryMvDnaCode',
            layoutCode: layoutCode,
            action: 'query'
          }
        };
        bus.emit("tabPage", tabPage);
      } else if (functionConfig.moduleCode === ConstDef.MODULE_CODE_FILTER) {
        const id = functionConfig.config.id;
        const layoutCode = "filterDefMvNdEL01";
        const tabPage = {
          name: "filterFunctionEdit",
          title: "查询功能编辑",
          moduleCode: ConstDef.MODULE_CODE_ENTRY,
          config: {
            nvId: id,
            ndCode: 'filterDefMvNd',
            layoutCode: layoutCode,
            action: 'query'
          }
        };
        bus.emit("tabPage", tabPage);
      }
    }
    return {
      editLayout,
      editNd,
      editDbMap,
      hasDbMapCode,
      hasFunctionCode,
      editFunction
    }
  }
}
</script>

<style scoped>

</style>
